package cn.wp.ewallet.wmps.dao.impl;

import cn.wp.common.core.dao.JdbcSupportTemplate;
import cn.wp.common.core.dao.SqlString;
import cn.wp.common.core.model.WpPage;
import cn.wp.ewallet.wmps.dao.EarnDetailLogDao;
import cn.wp.ewallet.wmps.model.entity.EarnDetailLogDo;
import cn.wp.ewallet.wmps.model.entity.EarnDetailLogDoo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by yangjin on 2020/1/9
 */
@Repository
public class EarnDetailLogDaoImpl implements EarnDetailLogDao {

    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    @Override
    public WpPage<EarnDetailLogDoo> selectEarnDetailLogList(int month, WpPage page, String earnStatus, String walletAddress) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("SELECT e.EARN_DETAIL_LOG_ID, e.EARN_DATE, e.DAILY_EARN, f.fina_prod_name");
        sqlString.add("FROM account_info a, prod_order_rel r, fina_prod_info f, earn_detail_log e");
        sqlString.add("WHERE e.prod_order_id = r.prod_order_id");
        sqlString.add("    AND r.fina_prod_id = f.fina_prod_id");
        sqlString.add("    AND e.account_id = a.account_id");
        sqlString.add("    AND a.wallet_address = ?");
        sqlString.add("    AND e.earn_status = ?");
        sqlString.add("    AND DATE_SUB(CURDATE(), INTERVAL ? MONTH) < e.EARN_DATE");
        Object[] params = new Object[] {
                walletAddress,
                earnStatus,
                month
        };
        return jdbcSupportTemplate.selectList(sqlString.build(),params, page, EarnDetailLogDoo.class);
    }

    @Override
    public EarnDetailLogDo selectEarnDetailLogByProId(String prodOrderId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select *");
        sqlString.add("from EARN_DETAIL_LOG a");
        sqlString.add("where a.PROD_ORDER_ID = ?");
        return jdbcSupportTemplate.selectObject(sqlString.build(), new Object[]{prodOrderId}, EarnDetailLogDo.class);
    }

    @Override
    public void update(EarnDetailLogDo earnDetailLogDo) {
        jdbcSupportTemplate.updateSelective(earnDetailLogDo);
    }

    @Override
    public void insert(EarnDetailLogDo earnDetailLogDo) {
        jdbcSupportTemplate.insert(earnDetailLogDo);
    }

    @Override
    public List<EarnDetailLogDo> selectEarnDetailLogByProIdAndStatus(String prodOrderId, String returnStatus) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select *");
        sqlString.add("from EARN_DETAIL_LOG a");
        sqlString.add("where a.PROD_ORDER_ID = ?");
        sqlString.add("  and a.EARN_STATUS= ?");
        return jdbcSupportTemplate.selectList(sqlString.build(),new Object[]{prodOrderId,returnStatus},EarnDetailLogDo.class);
    }

    @Override
    public void updateBabch(List<EarnDetailLogDo> earnDetailLogDos) {
        jdbcSupportTemplate.batchUpdate(earnDetailLogDos);
    }


    public Date generateFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date firstDay = calendar.getTime();
        return firstDay;
    }

}
